package com.flag.liutao.modules.flag.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.flag.domain.Unit;
import com.flag.liutao.modules.flag.mapper.UnitMapper;
import com.flag.liutao.modules.flag.param.UnitParam;
import com.flag.liutao.modules.flag.service.IUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.flag.vo.UnitDTO;
import com.flag.liutao.modules.flag.vo.UnitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-12
 */
@Service
public class UnitServiceImpl extends ServiceImpl<UnitMapper, Unit> implements IUnitService {

    @Autowired
    private UnitMapper unitMapper;

    @Override
    public Map<String, Object> getUnitPage(int page, int limit, String keyword) {
        IPage<UnitVo> iPage = new Page<>(page,limit);
        IPage<UnitVo> pageDB = unitMapper.getUnitPage(iPage,keyword);
        Map<String,Object> map = new HashMap<>();
        map.put("content",pageDB.getRecords());
        map.put("total", pageDB.getTotal());
        return map;
    }

    @Override
    public List<UnitVo> getUnitList() {
        List<Unit> unitList = unitMapper.selectList(null);
        List<UnitVo> convert = Convert.convert(new TypeReference<List<UnitVo>>() {}, unitList);
        return convert;
    }

    @Override
    public void addUnit(UnitParam param) {
        LambdaQueryWrapper<Unit> lqw  = new LambdaQueryWrapper<>();
        lqw.eq(Unit::getUnitName,param.getUnitName());
        Unit unit = unitMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(unit)){
            throw new MyException("分队名称："+param.getUnitName()+"已被使用");
        }
        Unit unitDB = new Unit();
        BeanUtil.copyProperties(param,unitDB);
        unitMapper.insert(unitDB);
    }

    @Override
    public void editUnit(UnitParam param) {
        Unit unit1 = unitMapper.selectById(param.getId());
        if(ObjectUtil.isNull(unit1)){
            throw new MyException("该分队不存在");
        }

        LambdaQueryWrapper<Unit> lqw  = new LambdaQueryWrapper<>();
        lqw.eq(Unit::getUnitName,param.getUnitName());
        Unit unit2 = unitMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(unit2) && param.getId()!=unit2.getId()){
            throw new MyException("分队名称："+param.getUnitName()+"已被使用");
        }

        Unit unitDB = new Unit();
        BeanUtil.copyProperties(param,unitDB);
        unitMapper.updateById(unitDB);
    }

    @Override
    public void delUnit(Long id) {
        unitMapper.deleteById(id);
    }

    @Override
    public List<UnitDTO> getUnitUserList() {
        return unitMapper.getUnitUserList();
}
}
